<!--检查结果录入-->

<template>
  <div class="app-container">
    <!-- 查询条件开始 -->
    <el-card style="margin-bottom:3px">
      <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
        <el-form-item label="检查项目" prop="checkItemIds">
          <el-checkbox v-model="checkAll" :indeterminate="isIndeterminate" @change="handleCheckAllChange">全选
          </el-checkbox>
          <div style="margin: 15px 0;"/>
          <el-checkbox-group v-model="queryParams.checkItemIds" @change="handleCheckedItemChange">
            <el-checkbox
                v-for="d in checkItemOptions"
                :key="d.checkItemId"
                :label="d.checkItemId"
                :value="d.checkItemId"
            >{{ d.checkItemName}}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="挂号单号" prop="regId">
          <el-input
              v-model="queryParams.regId"
              placeholder="请输入挂号单号"
              clearable
              size="small"
              style="width:380px"
          />
        </el-form-item>
        <el-form-item label="患者姓名" prop="patientName">
          <el-input
              v-model="queryParams.patientName"
              placeholder="请输入患者姓名"
              clearable
              size="small"
              style="width:380px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 查询条件结束 -->
    <!-- 数据表格开始 -->
    <el-card style="margin-bottom:3px">
      <el-table
          ref="refTable"
          v-loading="loading"
          border
          :data="checkResultList"
      >
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="价格">
                <span>{{ props.row.price }}</span>
              </el-form-item>
              <el-form-item label="创建时间">
                <span>{{ props.row.createTime }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
<!--        <el-table-column label="检查单位" align="center" prop="itemId" width="250"/>-->
        <el-table-column label="挂号单号" align="center" width="300" prop="regId"/>
        <el-table-column label="项目名称" align="center" prop="checkItemName"/>
        <el-table-column label="患者姓名" align="center" prop="patientName"/>
        <el-table-column
            prop="resultStatus"
            min-width="120"
            fixed="right"
            align="center"
            label="状态">
          <template slot-scope="scope">
            <div
              v-if="scope.row.resultStatus==='0'"
              >
              检测中
            </div>
            <div
              v-else-if="scope.row.resultStatus==='1'"
              >
              检测完成
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-edit" size="mini" @click="handleImportResult(scope.row)">
              录入结果
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页控件开始 -->
      <!--分页  -->
      <el-pagination
          :current-page="pageNo"
          :page-sizes="[5, 10, 20, 30]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
      <!-- 分页控件结束 -->
    </el-card>
    <!-- 数据表格结束 -->
    <!-- 录入检查结果弹层开始 -->
    <el-dialog
        :title="title"
        :visible.sync="open"
        width="1000px"
        center
        append-to-body
        @close="HandleCloseDialog"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="检查结果" prop="resultMsg">
          <el-input
              v-model="form.resultMsg"
              type="textarea"
              :rows="4"
              placeholder="请输入检查结果"
          />
        </el-form-item>
        <!-- 文件上传开始 -->
        <el-form-item label="结果上传">
          <el-upload
              style="width: 120px"
              class="avatar-uploader"
              action="http://172.16.2.59:7777/fileUpload/upload"
              name="file"
              :show-file-list="false"
              :on-success="handleFileSuccess"
              :before-upload="beforeFileUpload"
          >
            <img
                v-if="form.resultImg"
                :src="form.resultImg"
                class="avatar"
                style="width: 220px"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">上传头像图片大小不能超过 5MB</div>
          </el-upload>
        </el-form-item>


        <!-- 文件上传结束 -->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </span>
    </el-dialog>
    <!-- 录入检查结果弹层开始 -->

  </div>
</template>


<script>
export default {
  data(){
    return{
      // 遮罩
      loading: false,

      // 检查项目数据
      checkItemOptions: [],
      // 查询参数
      queryParams: {
        regId: undefined,
        patientName: undefined,
        checkItemIds: [],
        pageNo:undefined,
        //每页的大小
        pageSize:undefined,
      },
      // 总条数
      pageNo:1,
      //每页的大小
      pageSize:5,
      total:0,
      // 是否全选
      checkAll: true,
      // 是否为半选状态
      isIndeterminate: false,
      // 检查结果的状态字典
      resultStatusOptions: [],
      // 检查结果的数据
      checkResultList: [],
      // 是否打开录入结果的弹出层
      open: false,
      // 弹出层的标题
      title: '',
      // 要提交到后台的数据
      form: {
        cocId: undefined,
        resultMsg: undefined,
        resultImg: undefined
      },
      // 图片上传的请求头
      headers: {
        token: sessionStorage.getItem("token")
      },
      action: "http://172.16.2.59:7777/fileUpload/upload",
      imageUrl: '',
      // 声明上传路径
      uploadPath: undefined,
      // 控件里面的文件列表
      fileList: [],
      // 头
      // 文件列表的json对象
      fileListJsonObj: [],
      //上传文件的最大大小
      maxSize:2
    }
  },
  created() {
    //所有可用的检查项目
    this.$http.get("/checkItemjsh/queryAll").then(res=>{
      //console.log(res.data)
      this.checkItemOptions = res.data.data
      this.queryParams.checkItemIds = this.checkItemOptions.map(item => item.checkItemId)
      //this.loadTable()
    })

  },
  methods:{
    // 全选
    handleCheckAllChange(val) {
      this.queryParams.checkItemIds = val ? this.checkItemOptions.map(item => item.checkItemId) : []
      this.isIndeterminate = false
    },
    // 选择某一个项目
    handleCheckedItemChange(value) {
      const checkedCount = value.length
      this.checkAll = checkedCount === this.checkItemOptions.length
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.checkItemOptions.length
      //this.loadTable;
    },
    loadTable(){
      this.queryParams.pageNo=this.pageNo;
      this.queryParams.pageSize=this.pageSize;
      //console.log(JSON.stringify(this.queryParams)+"=====================")
      //{"regId":"GH1834503734795235328","patientName":"唐太宗","checkItemIds":[1,2,3,4,11,16],"pageNo":1,"pageSize":5}
      this.$http.post("/checkResultCheck/queryByParams",this.queryParams).then(res=>{
        //console.log("res.data:"+JSON.stringify(res.data)+"!!!!!!!!!!!!")
        this.checkResultList = res.data.data.list
        this.total = res.data.data.total
      })
    },

    // 录入结果
    handleImportResult(row) {
      // 打开录入结果的弹出层
      this.open = true
      this.title = '录入【' + row.patientName + '】的检查结果'
      this.form.cocId = row.cocId
    },
      // 完成检查
      handleSubmit()
      {
        const tx = this
        tx.$confirm('是否确定完成检查，数据提交之后不能再修改?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // console.log(JSON.stringify(tx.form)+"=====================")
          //{"cocId":"ITEM1834504236345913344","resultMsg":"1"}
          tx.$http.post("/checkResultCheck/completeTheChecks",tx.form).then(res => {
            tx.loadTable();
            // 关闭弹出层
            tx.cancel2()
          })
        })

    },


    //6,处理图片文件成功后的回调方法
    handleFileSuccess(res, file) {
      //回显上传图片
      this.imageUrl = URL.createObjectURL(file.raw);
      //给form的商品封面图片赋值 存储到数据库中
      this.form.resultImg = res.data;
    },
    //图片文件上传前的处理方法
    beforeFileUpload(file) {
      const isLt5M = file.size / 1024 / 1024 < 5;
      if (!isLt5M) {
        this.$message.error("上传头像图片大小不能超过 5MB!");
      }
      //返回false不会执行
      return isLt5M;
    },


    // 关闭
    cancel() {
      this.open = false
      this.form = {
        cocId: undefined,
        resultMsg: undefined,
        resultImg: undefined
      }

      this.$nextTick(() => {
        this.$refs.form.resetFields();
      });
    },
    // 关闭但不用删除已上传的图片
    cancel2() {
      this.open = false
      this.form = {
        itemId: undefined,
        resultMsg: undefined,
        resultImg: undefined
      }

      for (let i = this.fileList.length-1; i >=0; i--) {
        this.fileListJsonObj.splice(i, 1)
      }
      // this.$refs.upload1.clearFiles();
    },


    //录入检查弹窗关闭事件
    HandleCloseDialog(){
      this.form = {
        itemId: undefined,
        resultMsg: undefined,
        resultImg: undefined
      }

      for (let i = this.fileList.length-1; i >=0; i--) {
        //移除后后台同步删除图片
        imgDelete(this.fileListJsonObj[i].url).then((res) => {
        }).catch((err) => {
        });
        // this.fileList.splice(i, 1)
        this.fileListJsonObj.splice(i, 1)
      }
      // this.$refs.upload1.clearFiles();

    },




    // 查询
    handleQuery() {
      this.loadTable()
      this.showBottom = false
    },
    // 重置
    resetQuery() {
      // this.showBottom = false
      // this.queryParams.checkItemIds = this.checkItemOptions.map(item => item.checkItemId)
      this.checkAll = false
      this.queryParams.regId = undefined
      this.queryParams.checkItemIds = []
      this.queryParams.patientName = undefined
      this.loadTable()
    },

    // 分页pageSize变化时触发
    handleSizeChange(val) {
      this.pageSize = val;
      // 重新查询
      this.loadTable();
    },
    // 点击上一页  下一页，跳转到哪一页面时触发
    handleCurrentChange(val) {
      this.pageNo= val;
      // 重新查询
      this.loadTable();
    },
  }
}
</script>

<style scoped>
.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>